package com.my.blog.website.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Description 文件上传
 * @ClassName FileUtils
 * @Author gyh
 * @date 2019.12.11 17:28
 */
public class FileUtils {
    /***
     * 文件上传
     * @param file
     * @param filePath
     * @param fileName
     * @return
     * @throws Exception
     */
    public static Boolean uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        FileOutputStream out = null;
        try {
            File targetFile = new File(filePath);
            //如果目录不存在，创建目录
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            out = new FileOutputStream(filePath + fileName);
            out.write(file);
            out.flush();
            //写入成功
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            //写入失败
            return false;
        } finally {
            out.close();
        }
    }
}
